﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace M3UMax
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        public class M3U
        {
            public SortedList<string, List<M3UFileLine>> NameSourceList { get; set; }
            public List<M3UBaseLine> M3ULineList { get; set; }

            public M3U()
            {
                NameSourceList = new SortedList<string, List<M3UFileLine>>();
                M3ULineList = new List<M3UBaseLine>();
            }

            public static M3U Open(string pFileName)
            {
                return new M3U(pFileName);
            }

            public M3U(string pFileName)
            {
                NameSourceList = new SortedList<string, List<M3UFileLine>>();
                M3ULineList = new List<M3UBaseLine>();

                using (var sr = new System.IO.StreamReader(pFileName))
                {
                    while (!sr.EndOfStream)
                    {
                        AddLine(sr.ReadLine());
                    }
                }
            }

            public void AddLine(string pLine)
            {
                M3UBaseLine tM3ULine = null;

                if(pLine.StartsWith("#"))
                {
                    tM3ULine = new M3UTextLine(pLine);
                }
                else
                {
                    tM3ULine = new M3UFileLine(pLine);
                    var tName = ((M3UFileLine)tM3ULine).Name.ToUpper();
                    if (!NameSourceList.ContainsKey(tName))
                    {
                        NameSourceList.Add(tName, new List<M3UFileLine>());
                    }
                    NameSourceList[tName].Add(((M3UFileLine)tM3ULine));
                }
                tM3ULine.Index = M3ULineList.Count;
                M3ULineList.Add(tM3ULine);
            }

            public class M3UBaseLine
            {
                public int Index { get; set; }

            }

            public class M3UFileLine : M3UBaseLine
            {
                public M3UFileLine(string pLine)
                {
                    FileName = pLine;
                    Name = pLine.Substring(pLine.LastIndexOf("\\") + 1, pLine.Length - (pLine.LastIndexOf("\\") + 1));
                }
                public string Name { get; set; }
                public string FileName { get; set; }
                public override string ToString()
                {
                    
                    return FileName;
                }
            }

            public class M3UTextLine : M3UBaseLine
            {
                public M3UTextLine(string pLine)
                {
                    Text = pLine;
                }
                public string Text { get; set; }
                public override string ToString()
                {
                    return Text;
                }
            }

            public void ProcessSource(string pSource)
            {
                var tDir = new System.IO.DirectoryInfo(pSource);
                System.IO.FileInfo[] tMp3Files = tDir.GetFiles("*.mp3", System.IO.SearchOption.AllDirectories);

                foreach (System.IO.FileInfo tMp3File in tMp3Files)
                {
                    var tFindName = tMp3File.Name.ToUpper();

                    if (NameSourceList.ContainsKey(tFindName))
                    {
                        var tList = NameSourceList[tFindName];

                        foreach (var tItem in tList)
                        {
                            tItem.FileName = tMp3File.FullName;
                        }
                    }
                }


            }

            public void Save(string pFileName)
            {
                using (var sw = new System.IO.StreamWriter(pFileName))
                {
                    for (int i = 0; i < M3ULineList.Count; i++)
                    {
                        sw.WriteLine(M3ULineList[i].ToString());
                    }
                }
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            var tM3U = M3U.Open(txtSourceFileName.Text);
            tM3U.ProcessSource(txtSourceDir.Text);
            tM3U.Save(txtDestFileName.Text);

        }
    }
}
